layui.use(['form', 'jquery'], function (data) {
    var form = layui.form,
        $ = layui.jquery;

    // 加载供应商下拉框

    $.ajax({
        type: "get",
        url: ctx + "/supplier/selectSupplierToSelect",
        data: {},
        success: function (data) {
            var opt;
            if (data != null) {
                for (var i = 0; i < data.length; i++) {
                    opt = "<option value='" + data[i].id + "'  >" + data[i].name + "</option>";
                    $("#supplierId").append(opt);
                }
                form.render()
            }
        }
    });
    form.on("select(supplier)", function (data) {
        $.ajax({
            type: "get",
            url: ctx + "/supplier/selectMaterialBySupplerIdToAddPurchaseOrder",
            data: {
                supplierId: data.value
            },
            success: function (data) {
                if (data != null) {
                    for (var i = 0; i < data.length; i++) {
                        opt = "<option value='" + data[i].no + "'  >" + data[i].name + "</option>";
                        $("#materialNo").append(opt);
                    }
                    form.render()
                }
            }
        })

    })


    form.on("select(material)", function (data) {

        $.ajax({
            type: "get",
            url: ctx + "/supplier/selectMaterialNameByMaterialIdToAddPurchaseOrder",
            data: {
                materialNo: data.value
            },
            success: function (data) {
                if (data != null) {
                    $("#materialName").val(data);
                } else {
                    layer.msg(data.msg)
                }

            }
        })
    });

    form.on('submit(addPurchaseOrder)', function (data) {
        var index = layer.msg("数据提交中，请稍后", {
            icon: 16,
            time: false,
            shade: 0.8
        });
        var formData = data.field;
        $.ajax({
            type: "post",
            url: ctx + "/supplier/addPurchaseOrderRequest",
            data: formData,
            success: function (data) {
                if (data.code == 200) {
                    layer.msg(data.msg, {icon: 5});
                    layer.closeAll("iframe")
                    parent.location.reload()
                } else {
                    layer.msg(data.msg)
                }
            }, error: function (data) {
                layer.msg(data.msg)
            }
        });
        return false;
    })


})